﻿using AutoMapper.Internal.Mappers;
using Himp.Platform;
using Microsoft.AspNetCore.Authorization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.Guids;
using Volo.Abp.ObjectMapping;

namespace Himp.ShareData
{
    /// <summary>
    /// 部门信息
    /// </summary>
    //[Authorize(DataHubPermissions.DeptsGroupName)]
    public class DeptInfoService : PlatformAppService, IDeptInfoService
    {
        private IRepository<DeptInfo, Guid> _deptInfoRepository;
        private readonly DeptManager _deptmentManager;
        private readonly IDistributedEventBus _distributedEventBus;

        public DeptInfoService(IRepository<DeptInfo, Guid> deptInfoRepository
            , DeptManager deptmentManager
            , IDistributedEventBus distributedEventBus)
        {
            _deptInfoRepository = deptInfoRepository;
            _deptmentManager = deptmentManager;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        //[Authorize(DataHubPermissions.DeptGroup.DeptInfo)]
        public async Task<PagedResultDto<DeptInfoDto>> GetListAsync(DeptInfoInput input)
        {
            var iQuery = await _deptInfoRepository.GetQueryableAsync();
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.DeptCodg), i => i.DeptCodg == input.DeptCodg);
            iQuery = iQuery.OrderBy(i => i.Srtno);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<DeptInfo>, List<DeptInfoDto>>(iQuery.ToList());

                return new PagedResultDto<DeptInfoDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<DeptInfo>, List<DeptInfoDto>>(items.ToList());

                return new PagedResultDto<DeptInfoDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 根据Id获取科室详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        //[Authorize(DataHubPermissions.DeptGroup.DeptInfo)]
        public async Task<DeptInfoDto> GetAsync(Guid id)
        {
            var item = await _deptInfoRepository.GetAsync(id);
            var itemDto = ObjectMapper.Map<DeptInfo, DeptInfoDto>(item);

            return itemDto;
        }

        /// <summary>
        /// 添加科室信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        //[Authorize(DataHubPermissions.DeptGroup.DeptInfoCreate)]
        public async Task<DeptInfoDto> PostAsync(DeptInfoCreateDto input)
        {
            //var deptPropertyIds = input.DeptAssocAttrs?.Select(d => d.DeptAttrId).ToList() ?? new List<Guid>();
            //创建科室 
            var dept = _deptmentManager.CreateDept(input.ParentId, input.DeptCodg
                , input.DeptName, input.CatyCode, input.CatyName, input.OrganId, input.SrtNo
                , input.Begntime, input.Endtime, input.Itro);
            //_deptmentManager.SetDeptmentProperty(dept, deptPropertyIds);
            dept = await _deptInfoRepository.InsertAsync(dept);
            var itemDto = ObjectMapper.Map<DeptInfo, DeptInfoDto>(dept);
            //领域事件发布
            //var deptCreatedEto = ObjectMapper.Map<DeptInfo, DeptInfoCreatedEto>(dept);
            //await _distributedEventBus.PublishAsync(deptCreatedEto);

            return itemDto;
        }

        /// <summary>
        /// 修改科室信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        //[Authorize(DataHubPermissions.DeptGroup.DeptInfoEdit)]
        public async Task<DeptInfoDto> PutAsync(DeptInfoUpdateDto input)
        {
            var deptPropertyIds = input.DeptAssocAttrs?.Select(d => d.DeptAttrId).ToList() ?? new List<Guid>();
            var dept = await _deptInfoRepository.GetAsync(input.Id);
            dept.ChangeDept(input.DeptName, input.CatyCode, input.CatyName, input.OrganId, input.SrtNo
                , input.Begntime, input.Endtime, input.Itro);
            _deptmentManager.SetDeptmentProperty(dept, deptPropertyIds);
            dept = await _deptInfoRepository.UpdateAsync(dept);
            var itemDto = ObjectMapper.Map<DeptInfo, DeptInfoDto>(dept);

            //领域事件发布
            //var deptChangedEto = ObjectMapper.Map<DeptInfo, DeptInfoUpdatedEto>(dept);
            //await _distributedEventBus.PublishAsync(deptChangedEto);

            return itemDto;
        }

        /// <summary>
        /// 科室信息删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        //[Authorize(DataHubPermissions.DeptGroup.DeptInfoDelete)]
        public async Task DeleteAsync(Guid id)
        {
            var dept = await _deptInfoRepository.GetAsync(id);
            await _deptInfoRepository.DeleteAsync(id);

            //领域事件发布
            //var deptDeleteEto = ObjectMapper.Map<DeptInfo, DeptInfoDeletedEto>(dept);
            //await _distributedEventBus.PublishAsync(deptDeleteEto);
        }
    }
}
